﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Tools;
using Halcon二次开发.View.距离转换工具;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.距离转换工具
{
    public class DistanceToolController
    {
        /// <summary>
        /// 距离转换界面
        /// </summary>
        DistanceToolForm distanceToolForm;

        /// <summary>
        /// 控件句柄
        /// </summary>
        HWindow hWindon;
       public double Sum3 = 0;

        #region 输入输出参数
        public double[] HideIntputArrayDistances;

        public double[] HideOutputArrayDistances;
        #endregion

        public DistanceToolController(DistanceToolForm distanceToolForm,HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            //获取当前工位
            hWindon = hwindowFlowNode.hSmartWindowControl1.HalconWindow;
            this.distanceToolForm = distanceToolForm;
        }

        public DistanceToolController()
        {
        }

        bool paper = true;
        /// <summary>
        /// 运行
        /// </summary>
        public void Run()
        {
            if (paper)
            {
                Suan();
                paper = false;
            }
            distanceToolForm.textBox4.Text = "";
            try
            {
                if (HideIntputArrayDistances==null) { return; }
                string IntputDistance = null;
                foreach (double item in HideIntputArrayDistances)
                {
                    IntputDistance += "[" + item + "]";
                }
                distanceToolForm.textBox5.Text = IntputDistance;


                distanceToolForm.textBox4.Text = Sum3.ToString();

                string bilii = null;
                foreach (double item in HideIntputArrayDistances)
                {
                    bilii += "[" + item * Sum3 + "]";
                }
                distanceToolForm.textBox3.Text = bilii;

                HideOutputArrayDistances = new double[HideIntputArrayDistances.Length];

                for (int i = 0; i < HideOutputArrayDistances.Length; i++)
                {
                    HideOutputArrayDistances[i] = HideIntputArrayDistances[i]*Sum3;
                }
            }
            catch (Exception ex)
            {
                HideOutputArrayDistances = null;
                //MessageBox.Show("运算错误：" + ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw new Exception("运算错误：" + ex.Message);
            }
        }
        public static void HardRun(DistanceToolForm distanceToolForm, double[] HideIntputArrayDistances, ref double[] HideOutputArrayDistances)
        {
            // 提前解析并校验输入，减少异常的可能性
            if (string.IsNullOrWhiteSpace(distanceToolForm.textBox1.Text) || string.IsNullOrWhiteSpace(distanceToolForm.textBox2.Text))
            {
                throw new ArgumentException("输入值不能为空");
            }

            if (!double.TryParse(distanceToolForm.textBox1.Text.Trim(), out double Sum1) || Sum1 == 0)
            {
                throw new ArgumentException("必须是有效的非零数字");
            }

            if (!double.TryParse(distanceToolForm.textBox2.Text.Trim(), out double Sum2))
            {
                throw new ArgumentException("必须是有效数字");
            }

            double Sum3 = Sum2 / Sum1;

            try
            {
                // 检查输入数组是否有效
                if (HideIntputArrayDistances == null || HideIntputArrayDistances.Length == 0)
                {
                    HideOutputArrayDistances = Array.Empty<double>();
                    return;
                }

                // 直接操作数组，避免冗余的字符串拼接等无效操作
                int length = HideIntputArrayDistances.Length;
                HideOutputArrayDistances = new double[length];

                // 优化循环：减少多余的属性访问
                for (int i = 0; i < length; i++)
                {
                    double input = HideIntputArrayDistances[i];
                    HideOutputArrayDistances[i] = input * Sum3;
                }
            }
            catch (Exception ex)
            {
                HideOutputArrayDistances = null;
                throw new Exception("运算错误");
            }
        }
        public void Suan()
        {
            double Sum1 =double.Parse( distanceToolForm.textBox1.Text.Trim());
            double Sum2 = double.Parse(distanceToolForm.textBox2.Text.Trim());
             Sum3 = Sum2 / Sum1;
            distanceToolForm.textBox4.Text = Sum3.ToString();
        }
    }
    
}
